package cn.wolfcode.controller;

import cn.wolfcode.domian.Result;
import cn.wolfcode.domian.emp;
import cn.wolfcode.domian.pageBean;
import cn.wolfcode.service.empService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/emps")
@Slf4j
public class empController {
    @Autowired
    private empService empService;
      @GetMapping
     public Result list(@RequestParam(defaultValue = "1")Integer page,
                        @RequestParam(defaultValue = "10")Integer  pagesize,
                        String name,
                        Integer  gender,
                        @DateTimeFormat(pattern = "yyyy-MM-dd") Date begin,
                        @DateTimeFormat(pattern = "yyyy-MM-dd") Date end){
        log.info("查询所有员工");
   pageBean pageBean = empService.list(page,pagesize,name,gender,begin,end);
        return Result.success(pageBean);
    }

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Long> ids){
        log.info("删除员工");
        empService.delete(ids);
        return Result.success();
    }

    @PostMapping
    public Result add(@RequestBody emp emp){
        log.info("添加员工");
         empService.add(emp);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id){
          log.info("查询员工");
          emp emp = empService.getById(id);
          return Result.success(emp);
    }

    @PutMapping
    public Result update(@RequestBody emp emp){
        log.info("修改员工");
         empService.update(emp);
        return Result.success();
    }

    @PostMapping("/login")
    public Result login(@RequestBody emp emp){
          return empService.login(emp);
    }
}
